package com.quality.web.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.core.KeyIdentifier;
import com.smartgwt.client.widgets.*;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.layout.*;
import com.smartgwt.client.widgets.menu.*;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.tree.events.LeafClickEvent;
import com.smartgwt.client.widgets.tree.events.LeafClickHandler;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class QualityWeb implements EntryPoint {

  private static QualityWeb singleton;

  private ToolStrip topPanel;
  private SideNavigation sideNavigation;
  private TabSet tabSet;
  private Menu tabMenu;

  public static QualityWeb get() {
    return singleton;
  }

  public void onModuleLoad() {

    // top panel
    Label title = new Label("Quality Web");
    title.setStyleName("title");
    title.setWidth(350);
    
    final ImgButton homeButton = new ImgButton();
    homeButton.setSrc("home.png");
    homeButton.setWidth(32);
    homeButton.setHeight(32);
    homeButton.setPrompt("Quality Web");
    homeButton.setHoverStyle("interactImageHover");
    homeButton.setShowRollOver(false);
    homeButton.setShowDownIcon(false);
    homeButton.setShowDown(false);
    homeButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
      public void onClick(ClickEvent event) {
        com.google.gwt.user.client.Window.open(
            "http://code.google.com/p/quality-web/", "Quality Web", null);
      }
    });

    topPanel = new ToolStrip();
    topPanel.setHeight(48);
    topPanel.setWidth100();
    topPanel.addSpacer(200);
    topPanel.addMember(title);
    topPanel.addFill();
    topPanel.addMember(homeButton);
    topPanel.addSpacer(20);

    // side navigation panel
    VLayout sideNavigationLayout = new VLayout();
    sideNavigationLayout.setWidth(185);
    sideNavigationLayout.setHeight100();
    sideNavigationLayout.setShowResizeBar(true);

    sideNavigation = new SideNavigation();
    sideNavigation.addLeafClickHandler(new LeafClickHandler() {
      public void onLeafClick(LeafClickEvent event) {
      }
    });
    sideNavigationLayout.addMember(sideNavigation);

    // main panel
    tabMenu = createContextMenu();

    Layout paneContainerProperties = new Layout();
    paneContainerProperties.setLayoutMargin(0);
    paneContainerProperties.setLayoutTopMargin(5);

    HLayout mainPanel = new HLayout();
    mainPanel.setWidth100();
    mainPanel.setHeight100();

    Tab tab = new Tab();
    tab.setTitle("Home");
    tab.setPane(mainPanel);
    
    tabSet = new TabSet();
    tabSet.setWidth100();
    tabSet.setHeight100();
    tabSet.setContextMenu(tabMenu);
    tabSet.setPaneContainerProperties(paneContainerProperties);
    tabSet.addTabSelectedHandler(new TabSelectedHandler() {
      public void onTabSelected(TabSelectedEvent event) {
        Tab selectedTab = event.getTab();
        String historyToken = selectedTab.getAttribute("historyToken");
        if (historyToken != null) {
          History.newItem(historyToken, false);
        } else {
          History.newItem("main", false);
        }
      }
    });
    tabSet.addTab(tab);

    // compose bottom panel: side navigation panel + main panel
    HLayout bottomPanel = new HLayout();
    bottomPanel.setWidth100();
    bottomPanel.setHeight100();
    bottomPanel.setLayoutMargin(3);
    bottomPanel.addMember(sideNavigationLayout);
    bottomPanel.addMember(tabSet);

    // compose whole: top panel + bottom panel
    VLayout vLayout = new VLayout();
    vLayout.setWidth100();
    vLayout.setHeight100();
    vLayout.addMember(topPanel);
    vLayout.addMember(bottomPanel);
    vLayout.draw();

    // history
    History.addValueChangeHandler(new ValueChangeHandler<String>() {
      @Override
      public void onValueChange(ValueChangeEvent<String> event) {
      }
    });

    // remove loading dialog
    RootPanel.getBodyElement().removeChild(
        RootPanel.get("loadingWrapper").getElement());
  }

  // contextual menu for tab section: close; close all but current; close all
  private Menu createContextMenu() {
    Menu menu = new Menu();
    menu.setWidth(140);

    MenuItemIfFunction enableCondition = new MenuItemIfFunction() {
      public boolean execute(Canvas target, Menu menu, MenuItem item) {
        return tabSet.getSelectedTabNumber() != 0;
      }
    };

    MenuItem closeItem = new MenuItem("<u>C</u>lose");
    closeItem.setEnableIfCondition(enableCondition);
    closeItem.setKeyTitle("Alt+C");
    KeyIdentifier closeKey = new KeyIdentifier();
    closeKey.setAltKey(true);
    closeKey.setKeyName("C");
    closeItem.setKeys(closeKey);
    closeItem.addClickHandler(new ClickHandler() {
      public void onClick(MenuItemClickEvent event) {
        int selectedTab = tabSet.getSelectedTabNumber();
        tabSet.removeTab(selectedTab);
        tabSet.selectTab(selectedTab - 1);
      }
    });

    MenuItem closeAllButCurrent = new MenuItem("Close All But Current");
    closeAllButCurrent.setEnableIfCondition(enableCondition);
    closeAllButCurrent.addClickHandler(new ClickHandler() {
      public void onClick(MenuItemClickEvent event) {
        int selected = tabSet.getSelectedTabNumber();
        Tab[] tabs = tabSet.getTabs();
        int[] tabsToRemove = new int[tabs.length - 2];
        int cnt = 0;
        for (int i = 1; i < tabs.length; i++) {
          if (i != selected) {
            tabsToRemove[cnt] = i;
            cnt++;
          }
        }
        tabSet.removeTabs(tabsToRemove);
      }
    });

    MenuItem closeAll = new MenuItem("Close All");
    closeAll.setEnableIfCondition(enableCondition);
    closeAll.addClickHandler(new ClickHandler() {
      public void onClick(MenuItemClickEvent event) {
        Tab[] tabs = tabSet.getTabs();
        int[] tabsToRemove = new int[tabs.length - 1];

        for (int i = 1; i < tabs.length; i++) {
          tabsToRemove[i - 1] = i;
        }
        tabSet.removeTabs(tabsToRemove);
        tabSet.selectTab(0);
      }
    });

    menu.setItems(closeItem, closeAllButCurrent, closeAll);
    return menu;
  }

}